let layer;
let form;
layui.use(['layer', 'form'], function () {

    layer = layui.layer
    form = layui.form

    $("select").append(`<option value="">请选择文章类别</option>`)
    $.ajax({
        url: "/my/article/cates",
        success: function (res) {
            if (res.status == 0) {
                $.each(res.data, function (index, item) {
                    let one = $(`<option value=${item.Id}>${item.name}</option>`);

                    $("select").append(one);
                });
                form.render('select')
            }
        }
    });
    initEditor();

    $('#image').cropper({

        aspectRatio: 10 / 7,

        preview: '.img-preview'
    });


    $('#file_cp').on('click', function () {
        $('#file').click()
    })


    $('#file').on('change', function () {
        let obj = this.files[0]
        let src = URL.createObjectURL(obj)
        $('#image').cropper('replace', src)
    })

    $('form').on('submit', function (e) {
        e.preventDefault()
        let fd = new FormData(this)
        let canvas = $('#image').cropper('getCroppedCanvas', {
            width: 400,
            height: 280
        });
        canvas.toBlob(function (blob) {
            fd.append("cover_img", blob);
            $.ajax({
                url: "/my/article/add",
                type: "POST",
                data: fd,
                success: function (res) {
                    layer.msg(res.message);
                },
                processData: false, // 不转换
                contentType: false, // 不编码
            });
        });
    })
})










































//// ***********************************1.加载分类下拉选择
//let layer;
//let form;
//layui.use(['layer', 'form'], function() {
//  layer = layui.layer;
//  form = layui.form;

//  $("select").append(`<option value="">请选择文章类别</option>`)
//  $.ajax({
//    url: "/my/article/cates",
//    success: function(res) {
//      if (res.status == 0) {
//        $.each(res.data, function(index, item) {
//          let one = $(`<option value=${item.Id}>${item.name}</option>`);
//          $("select").append(one);
//        });

//        form.render('select'); //页面渲染元素重新渲染下
//      }
//    }
//  });
//})